package o;

import android.content.Context;
import android.content.res.AssetManager;
import android.net.Uri;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class dlx {
    private static int ˎ = 1;
    private static int ॱ = 0;

    private static String ˎ(String str, String str2) throws IOException {
        String canonicalPath = new File(str2, str).getCanonicalPath();
        if (canonicalPath.startsWith(new File(str2).getCanonicalPath())) {
            return canonicalPath;
        }
        throw new IOException("file path invalid.");
    }

    private static void ˊ(ZipInputStream zipInputStream, String str) throws IOException {
        Throwable th;
        Closeable closeable;
        byte[] bArr = new byte[4096];
        int i = 0;
        int i2 = 0;
        do {
            ZipEntry nextEntry = zipInputStream.getNextEntry();
            if (nextEntry != null) {
                String ˎ = ˎ(nextEntry.getName(), str);
                if (ˎ == null) {
                    throw new IOException("File name invalid.");
                }
                File file = new File(ˎ);
                if (nextEntry.isDirectory()) {
                    ˏ(file, true);
                } else {
                    ˏ(file, false);
                    try {
                        Closeable bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(ˎ), 4096);
                        while (i + 4096 <= 209715200) {
                            try {
                                int read = zipInputStream.read(bArr, 0, 4096);
                                if (read == -1) {
                                    break;
                                }
                                bufferedOutputStream.write(bArr, 0, read);
                                i += read;
                            } catch (Throwable th2) {
                                th = th2;
                                closeable = bufferedOutputStream;
                            }
                        }
                        bufferedOutputStream.flush();
                        ˊ(bufferedOutputStream);
                    } catch (Throwable th3) {
                        th = th3;
                        closeable = null;
                    }
                }
                zipInputStream.closeEntry();
                i2++;
                if (i2 > 8192) {
                    throw new IOException("Too many files to unzip.");
                }
            }
            return;
        } while (i <= 209715200);
        throw new IOException("File being unzipped is too big.");
        ˊ(closeable);
        throw th;
    }

    public static File ˎ(String str, String str2, Context context, boolean z) {
        Object obj = null;
        try {
            InputStream openInputStream;
            Object obj2;
            Closeable zipInputStream;
            File file;
            int i;
            int i2;
            if (!((Boolean) String.class.getMethod("startsWith", new Class[]{String.class}).invoke(str, new Object[]{"rpks/"})).booleanValue()) {
                int i3 = ॱ + 47;
                ˎ = i3 % DnsConfig.MAX_CACHE_ENTRIES;
                if (i3 % 2 == 0) {
                    if (!z) {
                        if (((Boolean) String.class.getMethod("startsWith", new Class[]{String.class}).invoke(str, new Object[]{"file:"})).booleanValue()) {
                            openInputStream = context.getContentResolver().openInputStream(Uri.parse(str));
                            if (openInputStream == null) {
                                obj2 = null;
                            } else {
                                obj2 = 1;
                            }
                            switch (obj2) {
                                case null:
                                    zipInputStream = new ZipInputStream(new BufferedInputStream(openInputStream));
                                    file = new File(str2);
                                    if (file.exists()) {
                                        if (file.mkdirs()) {
                                            i = 1;
                                        }
                                        switch (obj) {
                                            case null:
                                                ˊ(zipInputStream);
                                                return null;
                                        }
                                    }
                                    i2 = ॱ + 39;
                                    ˎ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                                    if (i2 % 2 == 0) {
                                        ˎ(file);
                                        i2 = ˎ + 5;
                                        ॱ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                                    } else {
                                        ˎ(file);
                                        i2 = ˎ + 5;
                                        ॱ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                                    }
                                    if (i2 % 2 != 0) {
                                    }
                                    ˊ(zipInputStream, str2);
                                    return file;
                                default:
                                    return null;
                            }
                        }
                        openInputStream = new FileInputStream(new File(Uri.parse(str).getPath()));
                        if (openInputStream == null) {
                            obj2 = 1;
                        } else {
                            obj2 = null;
                        }
                        switch (obj2) {
                            case null:
                                zipInputStream = new ZipInputStream(new BufferedInputStream(openInputStream));
                                file = new File(str2);
                                try {
                                    if (file.exists()) {
                                        i2 = ॱ + 39;
                                        ˎ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                                        if (i2 % 2 == 0) {
                                            ˎ(file);
                                            i2 = ˎ + 5;
                                            ॱ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                                        } else {
                                            ˎ(file);
                                            i2 = ˎ + 5;
                                            ॱ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                                        }
                                        if (i2 % 2 != 0) {
                                        }
                                    } else {
                                        if (file.mkdirs()) {
                                            i = 1;
                                        }
                                        switch (obj) {
                                            case null:
                                                ˊ(zipInputStream);
                                                return null;
                                        }
                                    }
                                    try {
                                        ˊ(zipInputStream, str2);
                                        return file;
                                    } catch (IOException e) {
                                        Logger.ˋ("UnzipsUtils", "unzip failed.");
                                        return file;
                                    } finally {
                                        ˊ(zipInputStream);
                                    }
                                } catch (Exception e2) {
                                    throw e2;
                                }
                            default:
                                return null;
                        }
                    }
                } else if (z) {
                    if (((Boolean) String.class.getMethod("startsWith", new Class[]{String.class}).invoke(str, new Object[]{"file:"})).booleanValue()) {
                        openInputStream = context.getContentResolver().openInputStream(Uri.parse(str));
                        if (openInputStream == null) {
                            obj2 = 1;
                        } else {
                            obj2 = null;
                        }
                        switch (obj2) {
                            case null:
                                zipInputStream = new ZipInputStream(new BufferedInputStream(openInputStream));
                                file = new File(str2);
                                if (file.exists()) {
                                    i2 = ॱ + 39;
                                    ˎ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                                    if (i2 % 2 == 0) {
                                        ˎ(file);
                                        i2 = ˎ + 5;
                                        ॱ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                                    } else {
                                        ˎ(file);
                                        i2 = ˎ + 5;
                                        ॱ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                                    }
                                    if (i2 % 2 != 0) {
                                    }
                                } else {
                                    if (file.mkdirs()) {
                                        i = 1;
                                    }
                                    switch (obj) {
                                        case null:
                                            ˊ(zipInputStream);
                                            return null;
                                    }
                                }
                                ˊ(zipInputStream, str2);
                                return file;
                            default:
                                return null;
                        }
                    }
                    openInputStream = new FileInputStream(new File(Uri.parse(str).getPath()));
                    if (openInputStream == null) {
                        obj2 = null;
                    } else {
                        obj2 = 1;
                    }
                    switch (obj2) {
                        case null:
                            zipInputStream = new ZipInputStream(new BufferedInputStream(openInputStream));
                            file = new File(str2);
                            if (file.exists()) {
                                if (file.mkdirs()) {
                                    i = 1;
                                }
                                switch (obj) {
                                    case null:
                                        ˊ(zipInputStream);
                                        return null;
                                }
                            }
                            i2 = ॱ + 39;
                            ˎ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                            if (i2 % 2 == 0) {
                                ˎ(file);
                                i2 = ˎ + 5;
                                ॱ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                            } else {
                                ˎ(file);
                                i2 = ˎ + 5;
                                ॱ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                            }
                            if (i2 % 2 != 0) {
                            }
                            ˊ(zipInputStream, str2);
                            return file;
                        default:
                            return null;
                    }
                }
            }
            AssetManager assets = context.getAssets();
            openInputStream = (InputStream) ((Class) avb.ˏ(5, 0, '㮃')).getMethod("ˏ", new Class[]{AssetManager.class, String.class}).invoke(null, new Object[]{str, assets});
            if (openInputStream == null) {
                obj2 = 1;
            } else {
                obj2 = null;
            }
            switch (obj2) {
                case null:
                    zipInputStream = new ZipInputStream(new BufferedInputStream(openInputStream));
                    file = new File(str2);
                    if (file.exists()) {
                        i2 = ॱ + 39;
                        ˎ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                        if (i2 % 2 == 0) {
                            ˎ(file);
                            i2 = ˎ + 5;
                            ॱ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                        } else {
                            ˎ(file);
                            i2 = ˎ + 5;
                            ॱ = i2 % DnsConfig.MAX_CACHE_ENTRIES;
                        }
                        if (i2 % 2 != 0) {
                        }
                    } else {
                        if (file.mkdirs()) {
                            i = 1;
                        }
                        switch (obj) {
                            case null:
                                ˊ(zipInputStream);
                                return null;
                        }
                    }
                    ˊ(zipInputStream, str2);
                    return file;
                default:
                    return null;
            }
        } catch (Exception e3) {
            Logger.e("UnzipsUtils", "open file input stream failed.");
            return null;
        } catch (Throwable th) {
            if (th.getCause() != null) {
            }
        }
    }

    public static boolean ˎ(File file) {
        int i = 0;
        if (file.exists()) {
            if (file.isFile()) {
                if (!file.delete()) {
                    Logger.e("UnzipsUtils", "delete file error");
                }
            } else if (file.isDirectory()) {
                File[] listFiles = file.listFiles();
                if (listFiles == null) {
                    return false;
                }
                int length = listFiles.length;
                while (i < length) {
                    ˎ(listFiles[i]);
                    i++;
                }
                if (!file.delete()) {
                    Logger.e("UnzipsUtils", "delete file dir error");
                }
            } else {
                Logger.ˏ("UnzipsUtils", "Other cases.");
            }
        }
        return true;
    }

    private static void ˏ(File file, boolean z) throws IOException {
        if (!file.exists()) {
            File parentFile = file.getParentFile();
            if (!parentFile.exists() && !parentFile.mkdirs()) {
                throw new IOException("create dir failed.");
            } else if (z) {
                if (!file.mkdir()) {
                    throw new IOException("create dir failed.");
                }
            } else if (file.createNewFile()) {
                Logger.ˏ("UnzipsUtils", "Other cases.");
            } else {
                throw new IOException("create file failed.");
            }
        }
    }

    public static void ˊ(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                Logger.e("UnzipsUtils", "An exception occurred while closing the 'Closeable' object.");
            }
        }
    }
}
